package yxy.game.pm2.active.module.sdk.weixin;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actzaixianfenxiang;
import yxy.cherry.game.base.bean.GatewayBean;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.ActOpenHandle;
import yxy.game.pm2.active.module.OpenType;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.sdk.WeiXinOtherParm;

import java.util.ArrayList;
import java.util.Map;

public class ActiveModule_在线礼包 extends PlayerModule {

	//bean -----------------------------------------------------------	
	public static final int acttype = 201;
	
	private DateTime update;	//用于决定是否刷新活动
	private long online;		//在线时长
 	private DateTime dot;		//打点时间
	private DateTime draw;		//上一个领取时间   默认为null
	private ArrayList<Integer> drawids = new ArrayList<Integer>();	//已经领取的在线礼包id
	
	public DateTime getUpdate() {
		return update;
	}
	public void setUpdate(DateTime update) {
		this.update = update;
	}
	public long getOnline() {
		return online;
	}
	public void setOnline(long online) {
		this.online = online;
	}
	public DateTime getDot() {
		return dot;
	}
	public void setDot(DateTime dot) {
		this.dot = dot;
	}
	public DateTime getDraw() {
		return draw;
	}
	public void setDraw(DateTime draw) {
		this.draw = draw;
	}
	public ArrayList<Integer> getDrawids() {
		return drawids;
	}
	public void setDrawids(ArrayList<Integer> drawids) {
		this.drawids = drawids;
	}
	
	//func ------------------------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

	
	/** 创建实例 **/
	private static ActiveModule_在线礼包 create(String playerCode) {
		ActiveModule_在线礼包 am = new ActiveModule_在线礼包();
		am.setPlayerCode(playerCode);
		am.setUpdate(DateTime.now());
		am.setDraw(DateTime.now());
		am.setDot(DateTime.now());
		return am;
	}
	
	/** 更新 **/
	private void update() {
		DateTime now = DateTime.now();
		int days = ActiveUtil.days(getUpdate(), now);
		if(days >= 1) {
			setUpdate(now);
			setDraw(now);
			setDot(now);
//			setLogin(now);
			setDrawids(new ArrayList<Integer>());
			set();
		}
	}

	/**
	 * 更新打点
	 */
	public void updatedot() {
		DateTime now = DateTime.now();
		
		RedisConnection redis = RedisManager.getInstance().getConnection(RedisType.REDIS_USER);
		PlayerBean pb = PlayerBean.get(getPlayerCode());
		try {
			GatewayBean gb = GatewayBean.find(redis, pb.getUid());
			
			if(getDot().getMillis() > gb.getLoginTime()) {
				//持续在线
				long time = now.getMillis() - getDot().getMillis();
				long onlinetime = getOnline() + time;
				setOnline(onlinetime);
			}
			
			if(getDot().getMillis() <= gb.getLoginTime()) {
				//上线
				long time = now.getMillis() - gb.getLoginTime();
				long onlinetime = getOnline() + time;
				setOnline(onlinetime);
			}
			
			setDot(now);
			set();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		
		
	}
	
	//redis -----------------------------------------------
	
	/** 获取redis key **/
	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	/** 保存 **/
	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取ActiveModule_征战日志
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_在线礼包 get(String playerCode) {
		try {
			PlayerBean pb = PlayerBean.get(playerCode);
			WeiXinOtherParm wxop = WeiXinOtherParm.get(pb.getUid());
			if(wxop == null) {
				return null;
			}
			
			ActiveModule_在线礼包 module = getv(key(playerCode));
			if (module == null) {
				module = create(playerCode);
			}

			module.update();
			module.check();
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 检查红点，活动是否开启 **/
	public void check() {
		Map<String, actzaixianfenxiang> datamap = ActDataCenter.getAllData(actzaixianfenxiang.class, getPlayerCode());
		if(datamap.size() == getDrawids().size()) {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.领完了);
			return;
		}else {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		}
	}
	
	
	//lock --------------------------------------------------------
	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
